package com.fh.modules.dbms.service.impl;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.fh.common.util.SpringContextUtils;
import com.fh.modules.dbms.model.DbSourceModel;
import com.fh.modules.dbms.service.DbSourceService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author qiujunda
 * @Date 22/12/2019
 * @Description
 */
@Service
public class DbSourceServiceImpl implements DbSourceService {

    @Override
    public List<DbSourceModel> queryAllDbSource() {
        List<DbSourceModel> list=new ArrayList<>();
        DynamicRoutingDataSource ds= (DynamicRoutingDataSource) SpringContextUtils.getBean("dataSource");
//        DynamicDataSourceContextHolder.setDataSourceLookupKey();
        Map dataSourceMap=ds.getCurrentDataSources();
        Iterator<String> iter = dataSourceMap.keySet().iterator();
        while(iter.hasNext()){
            DbSourceModel dbSourceModel=new DbSourceModel();
            dbSourceModel.setName(iter.next());
            list.add(dbSourceModel);
        }
        return list;
    }
}
